//
// Created by user on 2022/1/14.
//
#include <iostream>
#include <vector>
using namespace std;


vector<int> productExceptSelf(vector<int>& nums) {
//前缀乘积
int n = nums.size();
vector<int> prefix(n, 0);
    prefix[0]=1;
    for (int i = 1; i < n; ++i) {
        prefix[i] = prefix[i-1]*nums[i-1];
    }


    vector<int> suffix(n, 0);
    suffix[n-1]=1;
    for (int i = n-2; i >=0 ; i--) {
        suffix[i] = suffix[i+1]*nums[i+1];
    }


    vector<int> res(n, 0);
    for (int i = 0; i < n; ++i) {
        res[i] = prefix[i]*suffix[i];
    }

    return res;
}

int main(){
    vector<int> nums = {1,2,3,4};
    for (int num:productExceptSelf(nums)){
        cout<<num<<endl;
    }

    return 0;
}

